col4 BIT P2.7
col3 BIT P2.6
col2 BIT P2.5
col1 BIT P2.4
row1 BIT P2.3
row2 BIT P2.2
row3 BIT P2.1
row4 BIT P2.0
keydata EQU 70h
keybounc EQU 71h
keyclick EQU 72h
keyclear EQU 73h
puluhan EQU 74h
satuan EQU 75h
keyport EQU P2
ORG 0h
	MOV P2,#11111111b
	CALL Init_LCD
	CLR P3.6
	MOV R7, #0d
	MOV R6, #1d
	;MOV keyclick, #0FFh
Start: 	CALL keypad4x4 ;CALLing subroutine keypad4x4
	MOV A,keydata ;A = keydata
	CALL SaveRatusan
	CJNE A,#0FFh,WrLCD;
	SJMP Start ;LOOPING FOREVER PART 1
;
WrLCD: 	
	CJNE R7, #1d, Init_WrLCD
	MOV R1,#30h
	ADD A,R1
	MOV R1,A
	CALL Write_Data ;write data
	MOV keyclick, #01d
	SJMP START ;LOOPING FOREVER PART 2;
	
WrLCDRet: 	
	CJNE R7, #1d, Init_WrLCDRet
	MOV R1,#30h
	ADD A,R1
	MOV R1,A
	CALL Write_Data ;write data
	RET ;LOOPING FOREVER PART 2;

SaveRatusan:
	CJNE R6,#1d, SavePuluhan
	INC R6
	MOV R5, A
RET

SavePuluhan:
	CJNE R6,#2d, SaveSatuan
	INC R6
	MOV R4, A
RET

SaveSatuan:
	CJNE R6,#3d, Return
	MOV R3, A
RET

Return:
	CJNE A,#0FFh,WrLCD;
	SJMP Start
	
Init_WrLCD:
	MOV R1,#80h ;Pick DDRAM 1st row and 1st col
	CALL Write_Inst
	MOV R7, #1d
	SJMP WrLCD

Init_WrLCDRet:
	CALL Init_LCD
	MOV R1,#80h ;Pick DDRAM 1st row and 1st col
	CALL Write_Inst
	MOV R7, #1d
	SJMP WrLCDRet
	
Init_lcd:
	MOV R1,#00000001b ;Display clear
	CALL Write_Inst
	MOV R1,#00111000b ;Function set,Data 8 BIT,2 line font 5x7
	CALL Write_Inst
	MOV R1,#00001100b ;Display on,cursor off,cursor blink off
	CALL Write_Inst
	MOV R1,#00000110b ;Entry mode,Set increment
	CALL Write_Inst
	RET
;
Write_Inst:
	CLR P3.5 ;P3.5 = RS =0
	MOV P0,R1 ;copy R1 ke P0
	SETB P3.7 ;P3.7 =EN = 1
	CALL Delay ;CALL Delay time
	CLR P3.7 ;P3.7 =EN = 0
	RET
;
Write_Data:
	SETB P3.5 ;P3.5 = RS =1
	MOV P0,R1 ;copy R1 ke P0
	SETB P3.7 ;P3.7 =EN = 1
	CALL Delay ;CALL Delay time
	CLR p3.7 ;P3.7 =EN = 0
	RET

Clear_LCD:
	CALL Init_Lcd
	RET
;====================================
; subroutine scan keypad 4x4
;====================================
Keypad4x4:
	MOV keybounc,#50 ;keybounc = 50
	MOV keyport,#0FFh ;keyport=P2= FF
	CLR col4 ;col4 = 0

keyCOR: JB row4,keyMEN ; Key COR
	DJNZ keybounc,KeyCOR;
	MOV keydata,#0FFh ;Data Output
	RET

keyMEN: JB row3,keyUpA ; Key MEN
	DJNZ keybounc,keyMEN
	MOV keydata,#0FFh ;Data Output
	RET

keyUpA: JB row2,keyDnA ; Key Up Arrow
	DJNZ keybounc,keyUpA
	MOV keydata,#0FFh ;Data Output
	RET

keyDnA: JB row1,key3 ; Key Down Arrow
	DJNZ keybounc,keyDnA
	MOV keydata,#0FFh ;Data Output
	RET
;==========================================
key3: 	SETB col4
	CLR col3
	JB row4,key6
	DJNZ keybounc,key3 ; Key 3
	MOV keydata,#03d ;Data Output
	RET
;
key6: 	JB row3,key9
	DJNZ keybounc,key6 ; Key 6
	MOV keydata,#06d ;Data Output
	RET
;
key9: 	JB row2,keyENT
	DJNZ keybounc,key9 ; Key 9
	MOV keydata,#09d ;Data Output
	RET
;
keyENT: JB row1,key2
	DJNZ keybounc,keyENT ; Key ENT
	MOV keydata,#0FFh
	MOV A, keyclick
	CJNE A, #0h, Conversion
	;MOV keydata,#0eh ;Data Output
	RET
;============================================
key2: 	SETB col3 
	CLR col2
	JB row4,key5
	DJNZ keybounc,key2
	MOV keydata,#02d ;Data Output
	RET
;
key5: 	JB row3,key8
	DJNZ keybounc,key5
	MOV keydata,#05d ; Data Output
	RET
;
key8: 	JB row2,key0
	DJNZ keybounc,key8
	MOV keydata,#08d ;Data Output
	RET
;
key0: 	JB row1,key1
	DJNZ keybounc,key0
	MOV keydata,#00h ;Data Output
	RET
;==============================================
key1: 	SETB col2
	CLR col1
	JB row4,key4
	DJNZ keybounc,key1
	MOV keydata,#01d ;Data Output
	RET
;
key4: 	JB row3,key7
	DJNZ keybounc,key4
	MOV keydata,#04d ;Data Output
	RET
;
key7: 	JB row2,keyCAN
	DJNZ keybounc,key7
	MOV keydata,#07d ;Data Output
	
	RET
;
keyCAN: JB row1,Nokey
	DJNZ keybounc,keyCAN
	MOV keydata,#0FFh
	RET
	;MOV keydata,#0Fh ;Data Output
;

Nokey: 	MOV keydata,#0FFh
	RET
	
Conversion: 
	CALL Convert_To_Base36

	clr A
	MOV R3, A
	MOV R5, A
	MOV R4, A
	MOV R6, #1d
	MOV keyclick, #0h
	
	LJMP Start
	
Convert_To_Base36:
	CALL Init_LCD
	MOV R7, #0d
	;CALL Convert_To_Desimal
	;MOV A, R4
	;MOV B, #36d
	;DIV AB

	MOV A, R3
	CALL WrLCDRet
	MOV A, R4
	CALL WrLCDRet
	MOV A, R5
	CALL WrLCDRet
	
	;MOV puluhan, A
	;MOV satuan, B
	;MOV A, satuan
	;PUSH ACC
	;MOV A, puluhan
	;PUSH ACC
	
	;MOV R1,#80h
	;POP ACC
	
	;mov DPTR,#DataBase36
	;movc A,@A+DPTR
	;MOV A, R3
	;call write_inst
	;mov R1,A 
	;call write_data

	;MOV A, R4
	;call write_inst
	;mov R1,A 
	;call write_data

	;POP ACC
	;mov DPTR,#DataBase36
	;movc A,@A+DPTR
	;MOV A, R5
	;mov R1,#81h
	;call write_inst
	;mov R1,A 
	;call write_data
	
	RET

Convert_To_Desimal:
	MOV A, R4
	MOV B, #10d
	MUL AB
	MOV R4, A
	MOV A, R5
	MOV B, #100d
	MUL AB
	ADD A,R4
	ADD A,R3
	RET
;================================
;The end of Keypad 4x4 subroutine
;================================
Delay: 	MOV R0,#01h
Delay1: MOV R2,#50
	;DJNZ R2,$
	DJNZ R0,Delay1
	RET
	
DataBase36:
	db '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
END